<template>
    <div class="common mytest">
        <innerCommon 
            :location="location"
        >
        <template slot="left">
            <span>{{type==0?'测试列表':'考试列表'}}</span>
            <majorModule
                @submitSearch="submitSearch"
            ></majorModule>
        </template>
        <div slot="right">
            <a-button type="primary" @click="openTestBox()">{{type==0?'创建测试':'创建考试'}}</a-button>
        </div>
        <div slot="table" class="tableWrap">
            <a-table 
                size="middle"
                :columns="columns" 
                :data-source="list" 
                :pagination="pagination"
                :rowKey="record=>record.id"
                @change="changePage"
                :locale="{'emptyText': '暂无数据'}"
                bordered>
                <div slot="action" slot-scope="index,record" class="tableAction">
                    <span @click="previewTest(record.id)">预览</span>
                    <span @click="editTest(record)">修改</span>
                    <confirmBox
                        @handleOk="deleteTest(record.id)"
                        :typeName="'删除'"
                        :noticeInfo="record.title"
                        :noticeType="type==0?'测试':'考试'"
                    ></confirmBox>
                </div>
                <div slot="major" slot-scope="index,record">
                    {{record.majors[0].name}}
                </div>
                <div slot="module" slot-scope="index,record">
                    {{record.majors[1].name}}
                </div>
                <div slot="operator" slot-scope="index,record">
                    {{record.operator.name}}
                </div>
                <div slot="questionType" slot-scope="index,record">
                    <a-tag>
                        单选{{record.singleNum}}
                    </a-tag>
                    <a-tag>
                        多选{{record.multiNum}}
                    </a-tag>
                    <a-tag>
                        判断{{record.judgeNum}}
                    </a-tag>
                </div>
            </a-table>
        </div>
        </innerCommon>
        
        <!-- 弹出框一-->
        <a-modal
            :destroyOnClose='true'
            :confirmLoading="confirmLoading"
            :visible="testModal"
            :title="title"
            :okText="isAddTest==0?'创建':'修改'"
            cancelText="取消"
            @cancel="testModal=false"
            class="testModal"
            @ok="addTest()"
            >
                <div>
                    <div class="paperTypes" @click='typePaper' v-if="isAddTest==0">
                        <a-button icon="tool"  :type="btnType=='手工组卷'?'primary':'default'">手工组卷</a-button>
                        <a-button icon="cluster"  :type="btnType=='抽题组卷'?'primary':'default'">抽题组卷</a-button>
                    </div>
                    <eform
                    :formData="formData"
                    :fields="fields"
                    v-if="fields||isAddTest==0"
                    ref="form"
                    >
                        <div slot="major">
                            <a-select 
                                placeholder="请选择专业"
                                :notFoundContent="'暂无数据'"
                                @change="handleMajor"
                                :disabled="isAddTest==1?'disabled':false"
                                v-decorator="['major',{rules: [{ required: true, message: '请选择专业' }]}]"
                                >
                                <a-select-option :value="item.id" v-for="(item,index) in majorList" :key="index">
                                    {{item.name}}
                                </a-select-option>
                            </a-select>
                        </div>
                        <div slot="module">
                            <a-select 
                                placeholder="请选择模块"
                                :notFoundContent="'暂无数据'"
                                :disabled="isAddTest==1?'disabled':false"
                                v-decorator="['module',{rules: [{ required: true, message: '请选择模块' }]}]"
                                >
                                <a-select-option :value="item.id" v-for="(item,index) in moduleList" :key="index">
                                    {{item.name}}
                                </a-select-option>
                            </a-select>
                        </div>
                    </eform>
                </div>
        </a-modal>

        <!-- 弹出框二 -->
        <a-modal
            class="createPaper"
            style="top: 20px;"
            width="1400px"
            :destroyOnClose='true'
            :confirmLoading="confirmLoading"
            :visible="openQuestionBankBox"
            title="创建试卷"
            okText='创建'
            cancelText="取消"
            @cancel="openQuestionBankBox=false"
            @ok="btnType=='手工组卷'?CreatePaper():CreateRandomPaper()"
            >
                <div>
                    <div  style="display:flex;align-items: center;border-bottom:1px solid #ddd;padding-bottom:30px;">
                        <span style="font-size:16px;font-weight:bold;margin-right:20px;">{{openQuestionBankInfo.paperName}}</span>
                        <a-tag color="cyan">{{btnType}}</a-tag>
                        <a-tag color="orange">{{openQuestionBankInfo.majorName}}</a-tag>
                        <a-tag color="green">{{openQuestionBankInfo.moduleName}}</a-tag>
                    </div>
                    <div v-if="btnType=='手工组卷'">
                        <a-row>
                            <a-col :span="6" style="padding:15px 15px 15px 0">
                                <div class="table-wrapper">
                                    <a-table 
                                        size="small"
                                        bordered
                                        :columns="columns2"
                                        :dataSource="questionBank.tiku"
                                        :pagination='false'
                                        :customRow="clickRow"
                                        :rowKey="record=>record.id"
                                        :rowClassName="setRowClassName"
                                        >
                                    </a-table>
                                </div>
                            </a-col>
                            <a-col :span="14" class="border-style">
                                <div class="header-box">
                                    <a-button-group style="margin:0 30px;">
                                        <a-button @click="selectAll()"><a-checkbox :checked="selectAllBtn">全选</a-checkbox></a-button>
                                        <a-button icon='arrows-alt' @click="showAll">{{showBtnText}}</a-button>
                                    </a-button-group>
                                    <a-input-search
                                        placeholder="输入题目名称"
                                        style="width: 200px"
                                        @search="searchQuestion"
                                    />
                                    <a-select allowClear style="margin-left:10px;width: 120px" placeholder="全部题型" @change="selectKind">
                                        <a-select-option v-for="item in questionTypeList" :key="item.types" :value="item.types">{{item.value}}</a-select-option>
                                    </a-select>
                                </div>
                                <div class="question-content">
                                    <question
                                       :subjectList="questionBank.tikutimu"
                                       :isAction='false'
                                       @selectItem="selectItem"
                                    ></question>
                                </div>
                                <div>共{{questionBank.tikutimu.length}}题</div>
                            </a-col>
                            <a-col :span="4" class="number-box">
                                <a-list
                                    bordered
                                    :dataSource="questionTypeData"
                                >
                                    <div slot="header">按题型统计</div>
                                    <a-list-item slot="renderItem" slot-scope="item">
                                        <div>
                                            <p>{{item.type+' : '}}</p>
                                            <span>
                                            <a-tag color="cyan" style="margin-left:10px;">{{item.questionNumber}}题</a-tag>
                                            </span>
                                            <span>
                                                <a-input-number size="small" :min="0" :max="10" v-model="item.questionScore" @change="onChangeHandleQuestionScore" />
                                                分
                                            </span>
                                        </div>
                                    </a-list-item>
                                    <a-list-item>
                                        <div>
                                            <p>总计：</p>
                                            <a-tag color="cyan" style="margin-left:10px;">{{selectQuestion.length}}题</a-tag>
                                            <a-tag color="cyan">{{handleTotal}}分</a-tag>
                                        </div>
                                    </a-list-item>
                                   <a-list-item>
                                        <div>
                                            <p>及格分：</p>
                                            <a-input-number style="margin-left:10px;" size="small" :min="0" :max="handleTotal" v-model="score" @change="onChangePassing" />
                                                分
                                        </div>
                                    </a-list-item>
                                </a-list>
                            </a-col>
                        </a-row>
                    </div>
                    <div v-if="btnType=='抽题组卷'">
                        <a-row>
                            <a-col :span="6" style="padding:15px 15px 15px 0">
                                <div class="table-wrapper">
                                    <a-table 
                                    size="middle"
                                    bordered
                                    :pagination='false'
                                    :columns="columns3" 
                                    :dataSource="questionBank.tiku" 
                                    :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" />
                                </div>
                            </a-col>
                            <a-col :span="14" class="border-style">
                                <div class="random">
                                    <div class="randomStyle"><b>组卷规则：</b>根据题型设置出题数量和分值</div>
                                    <a-collapse v-if="bankList.length>0">
                                        <a-collapse-panel v-for="(item,index) in bankList" :key="index" :header="item.header">
                                            <div class="randomContent" v-for="(value,valueIndex) in item.children" :key="valueIndex">
                                                <span>{{value.itemName}}</span>
                                                <div>
                                                    <span>共计：{{value.total}} 题</span>
                                                    <span style="margin-left:20px;">
                                                        选取
                                                        <a-input-number 
                                                            :min="0" 
                                                            :max="value.total" 
                                                            @change="onChangeScore($event,index,valueIndex)"
                                                            :formatter="limitNumber "
                                                            :parser="limitNumber"
                                                        />
                                                        题
                                                    </span>
                                                </div>
                                            </div>
                                        </a-collapse-panel>
                                    </a-collapse>
                                    <a-empty description="暂无数据" v-else/>
                                </div>
                            </a-col>
                            <a-col :span='4'>
                                <ul class="static">
                                    <li>按题型统计</li>
                                    <li v-for="(item,index) in staticList" :key="index">
                                        <span>{{item.type}}：{{item.num}} 题</span><br/>
                                        <span>每题：<a-input-number style="width:65px;" :step="0.5" v-model="item.score" :min="1" :max="10"/> 分</span>
                                    </li>
                                    <li>
                                        <span>总计：{{totalNum}} 题</span><br/>
                                        <span>总分：{{totalScore}} 分</span>
                                    </li>
                                    <li>
                                        <span>及格分：<a-input-number style="width:65px;" v-model="score" :min="1" :max="totalScore"/> 分</span>
                                    </li>
                                </ul>
                            </a-col>
                        </a-row>
                    </div>
                </div>
        </a-modal>
    </div>
</template>
<script>
const limitNumber = value => {
    if (typeof value === 'string') {
      return !isNaN(Number(value)) ? value.replace(/^(0+)|[^\d]/g, '') : ''
    } else if (typeof value === 'number') {
      return !isNaN(value) ? String(value).replace(/^(0+)|[^\d]/g, '') : ''
    } else {
      return ''
    }
}
const columns=[
    {
        title: '序号',
        dataIndex: 'index',
        width:70
    },{
        title: '试题名称',
        dataIndex: 'title',
    },{
        title: '所属专业',
        scopedSlots: { customRender: 'major' },
    },{
        title: '所属教学模块',
        scopedSlots: { customRender: 'module' },
    },{
        title: '题目类型和数量',
        scopedSlots: { customRender: 'questionType' },
        width:200
    },{
        title: '总分',
        dataIndex: 'totalScore',
        width:100
    },{
        title: '操作者',
        scopedSlots: { customRender: 'operator' },
        width:100
    },{
        title: '创建时间',
        dataIndex: 'createTime',
        width:150
    },{
        title: '操作',
        scopedSlots: { customRender: 'action' },
        width:200
    }
]
const columns2=[
    {
        title: '序号',
        dataIndex: 'index',
        width:70
    },{
        title: '题库名称',
        dataIndex: 'title',
    }
]
const columns3 = [
    {
        title: '题库名称',
        dataIndex: 'title',
        key: 'name',
    }
];
import confirmBox from "@/components/common/confirmBox"
import innerCommon from "@/components/common/innerCommon"
import majorModule from "@/components/common/majorModule"
import eform from "@/components/common/form"
import question from "@/components/common/question"
import {
    getTestList,
    getMajorListApi,
    addTestApi,
    getQuestionItemApi,
    getSearchQuestionItemApi,
    createRandomPaperApi,
    deleteTestApi,
    updateTestApi,
    checkedIsEditApi
} from "@/request/api"
export default {
    name:'testAndExam',
    components:{innerCommon,confirmBox,majorModule,eform,question},
    props:{type:Number},
    watch:{
        searchValue:function(val){
            this.searchValue=val.trim()
        },
        selectQuestion:function(){
            let arr=JSON.parse(localStorage.getItem('selectQuestion'))
            this.questionTypeData[0].questionNumber = arr.filter(item=>item.type==0).length
            this.questionTypeData[1].questionNumber = arr.filter(item=>item.type==1).length
            this.questionTypeData[2].questionNumber = arr.filter(item=>item.type==2).length
            this.questionTypeData[3].questionNumber = arr.filter(item=>item.type==3).length
            return this.questionTypeData
        },
        selectedRowKeys:function(val){
            if(val.length==0){
                this.bankList=[]
                return
            }
            let arr=[]
            val.forEach(item=>{
                arr.push(this.questionBank.tiku[item].questions)
            })
            arr=arr.reduce((prev,next)=>{
                return [...prev,...next]
            })
            arr.forEach(item=>{
                if(item.type==0){
                    this.bankList[0]={
                        'header':'单选题：总计'+arr.filter(item=>item.type==0).length+'题',
                        'children':this.questionBank.tiku.filter((item,index)=>val.indexOf(index)!=-1).map(item=>({
                            'itemName':item.title,
                            'total':item.questions.filter(item=>item.type==0).length,
                            'isSelect':0,
                            'id':item.id
                        }))
                    }
                }else if(item.type==1){
                    this.bankList[1]={
                        'header':'多选题：总计'+arr.filter(item=>item.type==1).length+'题',
                        'children':this.questionBank.tiku.filter((item,index)=>val.indexOf(index)!=-1).map(item=>({
                            'itemName':item.title,
                            'total':item.questions.filter(item=>item.type==1).length,
                            'isSelect':0,
                            'id':item.id
                        }))
                    }
                }else if(item.type==2){
                    this.bankList[2]={
                        'header':'判断题：总计'+arr.filter(item=>item.type==2).length+'题',
                        'children':this.questionBank.tiku.filter((item,index)=>val.indexOf(index)!=-1).map(item=>({
                            'itemName':item.title,
                            'total':item.questions.filter(item=>item.type==2).length,
                            'isSelect':0,
                            'id':item.id
                        })),
                    }
                }
            })
        },
    },
    computed:{
        selectAllBtn:function(){
            if(this.questionBank.tikutimu.filter(item=>item.isSelect===false).length===0&&this.questionBank.tikutimu.length!==0){
                return true
            }else{
                return false
            }
        },
        // 手工组卷总分
        handleTotal:function(){
            let reg = /^[0-9]+.?[0-9]*/  //判断是否为数字
            let total=0
            this.questionTypeData.forEach(item=>{
                if(!reg.test(item.questionScore)){
                    item.questionScore=0
                }
                total+=item.questionNumber*item.questionScore
            })
            return total
        },
        totalNum:function(){
            return this.staticList.reduce((prev,next)=>{
                return prev+next.num
            },0)
        },
        totalScore:function(){
            return this.staticList.reduce((prev,next)=>{
                return prev+next.score*next.num
            },0)
        },
    },
    created(){
        this.location=[{
            name:"教学课堂管理",
            path:''
        },{
            name:this.type==0?"我的测试":'我的考试',
            path:'/mytest'
        }]
        this.init()
    },
    data(){
        let validatorNumber=(rule,value,callback)=>{
            let reg=/^[0-9]*$/
            if(reg.test(value)){
                callback()
            }else{
                callback(new Error('必须是数字'))
            }
        }
        const formData=[
            {
                label:this.type==0?"测试名称":"考试名称",
                prop:"title",
                placeholder:'请输入试题名称',
                rules:[{required:true,message:'请输入试题名称'}],
            },{
                label:"时长(分钟)",
                prop:"examTime",
                placeholder:'请输入时长',
                rules:[{required:true,message:'请输入时长'},{validator:validatorNumber}],
            },{
                label:"专业方向",
                scopedSlots: { customRender: 'major'},
            },{
                label:"教学模块",
                scopedSlots: { customRender: 'module'},
            }
        ]
        return{
            limitNumber,
            formData,
            location:[],
            columns,
            columns2,
            columns3,
            list:[],
            searchInfo:{
                page:1,
                pageSize:10,
                keyword:'',
                majorId:[],
                'type':0
            },
            pagination:{pageSize:10,total:0,current:1},
            searchValue:'',
            isAddTest:0,//0是新增 1是修改
            testModal:false,
            fields:null,
            majorList:[],
            moduleList:[],
            testItemId:null,
            confirmLoading:false,
            title:'',
            btnType:'手工组卷',
            questionBank:{tiku:[],tikutimu:[]},
            openQuestionBankBox:false,
            openQuestionBankInfo:{
                majorName:'',
                moduleName:'',
                paperName:''
            },
            selectQuestion:[],
            showBtnText:'展开',
            questionTypeList:[{value:'单选题',types:0},{value:'多选题',types:1},{value:'判断题',types:2},{value:'实操题',types:3}],
            handleSearchInfo:{
                type:'',
                keyword:'',
                questionLibId:''
            },
            score:0,
            testInfo:{},
            questionTypeData:[{type:'单选',questionNumber:0,questionScore:0},{type:'多选',questionNumber:0,questionScore:0},{type:'判断',questionNumber:0,questionScore:0},{type:'实操',questionNumber:0,questionScore:0}],
            selectedRowKeys: [],
            editTestId:'',
            bankList:[],
            staticList:[
                    {
                        type:'单选题',
                        num:0,
                        score:0
                    },{
                        type:'多选题',
                        num:0,
                        score:0
                    },{
                        type:'判断题',
                        num:0,
                        score:0
                    }],//侧边统计分数的信息
        }
    },
    methods:{
        init(){
            this.getToken(getTestList,{...this.searchInfo,type:this.type},res=>{
                this.list=res.data.rows.map((item,index)=>({...item,'index':(this.searchInfo.page-1)*10+index+1}))
                this.pagination.total=res.data.count
            })
            this.getToken(getMajorListApi,{},res=>{
                this.majorList=res.data
            })
        },
        submitSearch(data){
            this.searchInfo.keyword=data.keyword
            if(data.moduleId&&data.moduleId!=0){
                this.searchInfo.majorId=data.moduleId
            }else{
                this.searchInfo.majorId=data.majorId==0?null:data.majorId
            }
            this.init()
        },
        deleteTest(data){
            this.getToken(deleteTestApi,{'paperId':data},res=>{
                this.$message.success(res.message)
                this.init()
            })
        },
        changePage(data){
            this.searchInfo.page=data.current
            this.pagination.current=data.current
            this.init()
        },
        editTest(data){
            this.getToken(checkedIsEditApi,{'paperId':data.id},res=>{
                if(res.used){
                    this.$message.error('试卷已被课程选用，不能修改')
                }else{
                    this.$emit('toEdit',data)
                }
            })
        },
        openTestBox(){
            this.questionTypeData=[{type:'单选',questionNumber:0,questionScore:0},{type:'多选',questionNumber:0,questionScore:0},{type:'判断',questionNumber:0,questionScore:0},{type:'实操',questionNumber:0,questionScore:0}]
            this.score=0
            this.fields=null
            this.testModal=true;
            this.isAddTest=0
            this.title=this.type==0?"创建测试":"创建考试"
        },
        searchQuestion(data){
            this.handleSearchInfo.keyword=data
            this.handleSearch()
        },
        addTest(){
            this.$refs.form.form.validateFields().then(formDataRes=>{
                if(this.isAddTest==0){
                    let submit=()=>{
                        this.testInfo=formDataRes
                        this.confirmLoading=true
                        let formData={'majorId':formDataRes.module,'type':''}
                        getQuestionItemApi(formData).then(res=>{
                            this.confirmLoading=false
                            if(res.data.length>0){
                                this.testModal=false;
                                this.openQuestionBankBox=true
                                this.questionBank.tiku=res.data.map((item,index)=>({...item,'index':index+1}))
                                this.openQuestionBankInfo={
                                    'paperName':formDataRes.title,
                                    'majorName':this.majorList.filter(item=>item.id==formDataRes.major)[0].name,
                                    'moduleName':this.moduleList.filter(item=>item.id==formDataRes.module)[0].name
                                }
                                this.questionBank.tiku[0].isHover=true
                                this.handleSearchInfo.questionLibId=this.questionBank.tiku[0].id
                                this.questionBank.tikutimu=res.data[0].questions.map((item)=>({...item,isSelect:false,...item,isShowAnswer:false}))
                                localStorage.setItem('selectQuestion',JSON.stringify([]))
                            }else{
                                this.$message.error('该模块下无题库')
                            }
                        })
                    }
                    if(this.btnType=='手工组卷'){
                        this.submitPaperInfo={...formDataRes,'paper_type':1}
                        submit()
                    }else if(this.btnType=='抽题组卷'){
                        this.submitPaperInfo={...formDataRes,'paper_type':2}
                        submit()
                    }else{
                        this.$message.error('请选择组卷类型')
                    }
                }else{
                    this.getToken(updateTestApi,{...formDataRes,'paperId':this.editTestId},res=>{
                        this.testModal=false
                        this.$message.success(res.message)
                        this.init()
                    })
                }
            })
        },
        // 选择试卷类型e
        typePaper(e){
            this.btnType=e.target.innerText
        },
        handleMajor(data){
            this.moduleList=this.majorList.filter(item=>item.id==data)[0].children
            this.$refs.form.form.setFieldsValue({['module']:undefined})
        },
        // 修改每题的分数
        onChangeHandleQuestionScore(data){
            let reg = /^[0-9]+.?[0-9]*/  //判断是否为数字
            if(!reg.test(data)){
                this.$message.error('请输入数字')
            }
        },
        // 预览试卷
        previewTest(id){
            let routeData = this.$router.resolve({
                path: "/paperPreview",
                query: {id}
                });
                let width=800
                let left=parseInt((screen.availWidth/2) - (width/2))
                window.open(routeData.href, '_blank',"height=800, width="+width+", toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no,top=50,left="+left);
        },
        // 全选
        selectAll(){
            this.selectQuestion=JSON.parse(localStorage.getItem('selectQuestion'))
            if(!this.selectAllBtn){
                this.questionBank.tikutimu.map(item=>item.isSelect=true)
                this.questionBank.tikutimu.forEach(item=>{
                    if(this.selectQuestion.findIndex(value=>value.id==item.id)==-1){
                        this.selectQuestion.push(item)
                    }
                })
            }else{
                this.questionBank.tikutimu.map(item=>item.isSelect=false)
                this.questionBank.tikutimu.forEach(item=>{
                    if(this.selectQuestion.findIndex(value=>value.id==item.id)!=-1){
                        this.selectQuestion.splice(this.selectQuestion.findIndex(value=>value.id==item.id),1)
                    }
                })
            }
           localStorage.setItem('selectQuestion',JSON.stringify(this.selectQuestion))
        },
        selectKind(data){
            this.handleSearchInfo.type=data
            this.handleSearch()
        },
        handleSearch(){
            this.getToken(getSearchQuestionItemApi,this.handleSearchInfo,res=>{
                this.onSearchQuestion(res.data[0].questions)
            })
            
        },
        // 全部展开
        showAll(){
            if(this.questionBank.tikutimu.filter(item=>item.isShowAnswer==false).length!=0){
                this.questionBank.tikutimu.map(item=>item.isShowAnswer=true)
                this.showBtnText='收起'
            }else{
                this.questionBank.tikutimu.map(item=>item.isShowAnswer=false)
                this.showBtnText='展开'
            }
        },
        // 选择某一个
        selectItem(index){       
            this.selectQuestion= JSON.parse(localStorage.getItem('selectQuestion'))
            let selectQues=this.questionBank.tikutimu[index]
            if(this.selectQuestion.findIndex(item=>item.id==selectQues.id)!=-1){
                selectQues.isSelect=false
                this.selectQuestion.splice(this.selectQuestion.findIndex(item=>item.id==selectQues.id),1)
            }else{
                selectQues.isSelect=true
                this.selectQuestion.push(this.questionBank.tikutimu[index])
            }
            localStorage.setItem('selectQuestion',JSON.stringify(this.selectQuestion))
        },
        // 题目列表点击某一行时
        clickRow(record,index){
            return {
                on:{
                    click: function(){
                        let tikutimu=this.questionBank.tiku.filter(item=>item.id==record.id)[0].questions
                        this.questionBank.tiku = this.questionBank.tiku.map(({ ...q}, i)=>{
                            return i === index? {...q, isHover: true}: {...q, isHover: false}
                        })
                        this.handleSearchInfo.questionLibId=record.id
                        this.onSearchQuestion(tikutimu)
                    }.bind(this)
                }
            };
        },
        setRowClassName(record){
            if(record.isHover){
                return "hover-tr"
            }
        },

        onSearchQuestion(data){
            this.selectQuestion=JSON.parse(localStorage.getItem('selectQuestion'))
                this.showBtnText='展开'
                this.questionBank.tikutimu=data.map((item)=>({...item,isSelect:false,isShowAnswer:false}))
                this.questionBank.tikutimu.forEach(item=>{
                    if(this.selectQuestion.filter(value=>value.id==item.id).length!=0){
                        item.isSelect=true
                    }
                })
        },
        // 修改及格分
        onChangePassing(data){
            let reg = /^[0-9]+.?[0-9]*/  //判断是否为数字
            if(!reg.test(data)){
                this.$message.error('请输入数字')
                this.score=0
            }
        },
        onChangeScore(value,index,valueIndex){
            this.bankList[index].children[valueIndex].isSelect=value
            this.staticList[index].num=this.bankList[index].children.reduce((prev,next)=>prev+next.isSelect,0)
        },
        // 抽题组卷页
        onSelectChange (selectedRowKeys) {
            this.selectedRowKeys = selectedRowKeys
        },
        // 提示：及格分要小于总分
        lessTotal(){
            if(this.score>this.totalScore){
                return false
            }else{
                return true
            }
        },
        CreatePaper(){
            if(this.selectQuestion.length==0){
                this.$message.error('试卷题目不能为0')
            }else if(this.handleTotal==0){
                this.$message.error('总分不能为0')
            }else if(this.score==0){
                this.$message.error('及格分不能为0')
            }else{
                let formData={
                    'majorId':[this.testInfo.major,this.testInfo.module],
                    'title':this.testInfo.title,
                    'examTime':this.testInfo.examTime,
                    'question':localStorage.getItem('selectQuestion'),
                    'perSingle':this.questionTypeData[0].questionScore,
                    'perMulti':this.questionTypeData[1].questionScore,
                    'perJudge':this.questionTypeData[2].questionScore,
                    'passScore':this.score,
                    'totalScore':this.handleTotal,
                    'type':this.type,
                    'formedBy':0
                }
                this.getToken(addTestApi,formData,res=>{
                    this.$message.success(res.message)
                    this.openQuestionBankBox=false
                    this.init()
                })
            }
        },
        CreateRandomPaper(){
            if(this.totalNum==0){
                this.$message.error('试卷题目不能为0')
            }else if(this.totalScore==0){
                this.$message.error('总分不能为0')
            }else if(this.score==0){
                this.$message.error('及格分不能为0')
            }else{
                this.confirmLoading=true
                let newQuestion=this.questionBank.tiku.map(item=>({
                    'libId':item.id,
                    'singleNum':0,
                    'multiNum':0,
                    'judgeNum':0,
                }))
                newQuestion.forEach(item=>{
                    item.singleNum=this.bankList[0].children.filter(value=>value.id==item.libId)[0]?this.bankList[0].children.filter(value=>value.id==item.libId)[0].isSelect:0
                    item.multiNum=this.bankList[1].children.filter(value=>value.id==item.libId)[0]?this.bankList[1].children.filter(value=>value.id==item.libId)[0].isSelect:0
                    item.judgeNum=this.bankList[2].children.filter(value=>value.id==item.libId)[0]?this.bankList[2].children.filter(value=>value.id==item.libId)[0].isSelect:0
                })
                if(this.lessTotal()){
                    let formData={
                        'majorId':[this.testInfo.major,this.testInfo.module],
                        'title':this.testInfo.title,
                        'examTime':this.testInfo.examTime,
                        'perSingle':this.staticList[0].score,
                        'perMulti':this.staticList[1].score,
                        'perJudge':this.staticList[2].score,
                        'passScore':this.score,
                        'totalScore':this.totalScore,
                        'type':this.type,
                        'question':newQuestion,
                        'formedBy':1
                    }
                    createRandomPaperApi(formData).then(res=>{
                        this.$message.success(res.message)
                        this.openQuestionBankBox=false
                        this.init()
                    })
                }else{
                    this.$message.error('及格分要小于总分')
                }
                this.confirmLoading=false
            }
        }
    }
}
</script>
<style>
.createPaper  .ant-table-small > .ant-table-content > .ant-table-body {
    margin: 0;
}
.table-wrapper .ant-table-tbody .hover-tr{
    background: #E6F7FF;
}
.table-wrapper .ant-table-tbody tr{
    cursor:pointer
}
.createPaper .ant-modal-body{
    min-height:500px;
}
.createPaper .question-content,.random[data-v-751b1f52] {
    height: 450px;
    overflow: scroll;
}
.createPaper .ant-collapse-content > .ant-collapse-content-box{
    padding:0;
}
</style>
<style lang="stylus" scoped>
.testModal
    #eform
        .ant-col-6
            width:25%
.number-box .ant-input-number{
    width:51px;
}
.number-box .ant-list-bordered .ant-list-item{
    padding-left:12px;
    padding-right:12px;
}
.number-box .ant-tag{
    line-height:23px;
    height:23px;
}
.paperTypes
    margin:10px 0 30px;
    text-align:center
    button
        margin:0 20px;
.border-style
    border-top:0
    min-height:500px;
    .header-box
        display:flex;
        align-items: center;
        height:50px;
        border-left:1px solid #ddd;
.number-box
    padding:15px 0 0 15px;
.random
    .randomStyle
        padding:10px 20px
        border-bottom:1px solid #ddd
        margin-bottom:0
        .randomChild
            padding:10px 30px
    .ant-input-number
        border:1px solid #5be5f3
        width:70px;
    .ant-tag
        font-size:14px;
        line-height:28px;
        height:28px;
    .randomContent
        display:flex;
        justify-content:space-between;
        line-height:40px;
        border-bottom:1px solid #ddd;
        padding:0 10px;
.createPaper
    .static
        list-style:none;
        padding:15px;
        padding-right:0;
        li
            line-height:40px;
            border:1px solid #ddd;
            border-top:0;
            padding:5px 20px;
            &:nth-child(1)
                border-top:1px solid #ddd;
                font-weight:bold;
                text-align:center;

</style>